package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/mysql/models"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 药品修改
func (l *UpdateLogic) Update(in *doctorrpc.UpdateRequest) (*doctorrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	shop := models.Shop{
		Model:    gorm.Model{ID: uint(in.ShopId)},
		Name:     in.Name,
		Num:      int(in.Num),
		Address:  in.Address,
		Title:    in.Title,
		Fans:     in.Fans,
		DoctorId: in.DoctorId,
	}

	if in.DoctorId != shop.DoctorId {
		return nil, errors.New("你无权限更改药品信息")
	}

	models.DB.Debug().Updates(&shop)

	return &doctorrpc.UpdateResponse{
		ShopId: int64(shop.ID),
	}, nil
}
